package com.cictec.middleware.tde.rm.protocol.rm.decode;

import java.nio.ByteBuffer;

import org.apache.mina.core.session.IoSession;

import com.cictec.middleware.commons.utils.BinaryUtils;
import com.cictec.middleware.tde.exception.MessageDecoderException;
import com.cictec.middleware.tde.rm.protocol.AbstractMessageDecoder;
import com.cictec.middleware.tde.rm.protocol.TerminalMessage;
import com.cictec.middleware.tde.rm.protocol.TerminalMessage.Header;
import com.cictec.middleware.tde.rm.protocol.annotion.TerminalMessageDecoder;
import com.cictec.middleware.tde.rm.protocol.rm.message.Rm0x4BTerminalMessage;
import com.cictec.middleware.tde.rm.protocol.rm.utils.RmBinaryUtils;

/**
 * 
 * 0x4B 升级报站文件后设备返回结果
 * 
 * @author wsl
 *
 */
@TerminalMessageDecoder(0x4B)
public class Rm0x4BDecoder extends AbstractMessageDecoder {

	@Override
	public TerminalMessage decode(Header header, ByteBuffer in, IoSession session) throws MessageDecoderException {
		// TODO Auto-generated method stub
		Rm0x4BTerminalMessage tubfr = new Rm0x4BTerminalMessage(header);
		tubfr.setCreateTime(RmBinaryUtils.dwordToDateRM(in));
		tubfr.setSessIdLength(BinaryUtils.unsignedByteToInt(in));
		tubfr.setSessId(BinaryUtils.bytesToString(in, tubfr.getSessIdLength()));
		tubfr.setResult(BinaryUtils.unsignedByteToInt(in));
		tubfr.setFailureReason(BinaryUtils.unsignedByteToInt(in));
		tubfr.setUpdateType(BinaryUtils.unsignedByteToInt(in));
		tubfr.setDeviceModelLengh(BinaryUtils.unsignedByteToInt(in));
		tubfr.setDeviceModel(BinaryUtils.bytesToString(in, tubfr.getDeviceModelLengh()));
		tubfr.setAfterUpdateVerLen(BinaryUtils.unsignedByteToInt(in));
		tubfr.setAfterUpdateVer(BinaryUtils.bytesToString(in, tubfr.getAfterUpdateVerLen()));
		tubfr.setCrc(BinaryUtils.dwordToLong(in));
		return tubfr;
	}

}
